package com.boco.webmaster.model;

import java.util.Map;
import java.util.HashMap;

import com.boco.transnms.server.bo.base.BoHomeFactory;
import com.boco.transnms.server.bo.base.BoName;
import com.boco.transnms.server.bo.helper.cm.*;
import com.boco.transnms.server.bo.ibo.cm.INmsSystemBOX;
import com.boco.transnms.server.bo.ibo.cm.IVipBOX;
import com.boco.transnms.client.model.base.BoCmdFactory;
import com.boco.transnms.client.model.base.IBoCommand;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.common.dto.Vp;
import java.util.Iterator;

public class VipModel {
    private static VipModel instance = new VipModel();
    private Map vpMap = new HashMap();
    public VipModel() {
        refresh();
    }

    public void refresh(){
    	IVipBOX ibo = (IVipBOX)BoHomeFactory.getInstance().getBO(BoName.VipBOX);
        DataObjectList dbos = ibo.getAllVP(new BoActionContext());
        for (int i = 0; i < dbos.size(); i++) {
            Vp dbo = (Vp)dbos.get(i);
            vpMap.put(dbo.getCuid(),dbo);
        }
    }

    public static VipModel getInstance() {
        return instance;
    }

    public Vp getVpByCuid(String cuid){
        return (Vp)vpMap.get(cuid);
    }

    public String getCuidByLabelCn(String label_cn){
        Iterator i = vpMap.keySet().iterator();
        while (i.hasNext()) {
            String cuid = (String) i.next();
            Vp dbo = (Vp) vpMap.get(cuid);
            if(dbo.getLabelCn().equals(label_cn)){
                return dbo.getCuid();
            }
        }
        return null;
    }

    public DataObjectList getAllVp(){
        DataObjectList dbos = new DataObjectList();
        Iterator i = vpMap.keySet().iterator();
        while (i.hasNext()) {
            String cuid = (String) i.next();
            Vp dbo = (Vp) vpMap.get(cuid);
            dbos.add(dbo);
        }
        return dbos;
    }

    public void addVp(Vp dbo){
        vpMap.put(dbo.getCuid(),dbo);
    }

    public void delVp(Vp dbo){
        vpMap.remove(dbo.getCuid());
    }

    public void delVpByObjectId(Long objectid) {
        Iterator iterator = vpMap.keySet().iterator();
        while (iterator.hasNext()) {
            String vendor_cuid = (String) iterator.next();
            Vp dbo = (Vp) vpMap.get(vendor_cuid);
            if (dbo.getObjectNum() == objectid) {
                iterator.remove();
            }
        }
    }

    public Vp getVpByObjectId(Long objectId){
        Vp vp=null;
        Iterator iterator = vpMap.keySet().iterator();
        while(iterator.hasNext()){
            String vp_cuid = (String)iterator.next();
            Vp dbo = (Vp)vpMap.get(vp_cuid);
            if(dbo.getObjectNum()==objectId){
                vp = getVpByCuid(vp_cuid);
                break;
            }
        }
        return vp;
    }


    public void modifyVp(Vp dbo){
        vpMap.remove(dbo.getCuid());
        vpMap.put(dbo.getCuid(),dbo);
    }

    public String getVpCuidByRelatedDistrictCuidAndLabelcn(String relatedDistrictCuid,String labelcn){
        Iterator i = vpMap.keySet().iterator();
        while (i.hasNext()) {
            String cuid = (String) i.next();
            Vp dbo = (Vp) vpMap.get(cuid);
             if(dbo.getRelatedDistrictCuid().equals(relatedDistrictCuid) && dbo.getLabelCn().equals(labelcn)){
                return dbo.getCuid();
            }
        }
        return null;
    }
}
